﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace Z2.Core
{
	
	#region Using

	using System;
	using System.Collections.Generic;
	using Microsoft.Xna.Framework.Input;

	#endregion

	#region Class KeyEventArgs

	/// <summary>
	/// Argument události KeyPressed pro třídu <see cref="KeyboardControl"/>
	/// </summary>
	public class KeyEventArgs : EventArgs
	{

		#region Members
		private Keys m_key;
		#endregion

		#region Constructor
		/// <summary>
		/// Konstruktor
		/// </summary>
		/// <param name="key">Stlačená klávesa</param>
		public KeyEventArgs(Keys key)
		{
			this.m_key = key;
		}
		#endregion

		#region Properties

		/// <summary>
		/// Vrací stlačenou klávesu
		/// </summary>
		public Keys Key
		{
			get
			{
				return this.m_key;
			}
		}

		#endregion

	}

	#endregion

	/// <summary>
	/// Třída pro ovládání 
	/// </summary>
	internal class KeyboardControl
	{

		#region Members

		private KeyboardState m_state;
		private List<Keys> m_pressed;

		#endregion

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		public KeyboardControl()
		{
			this.m_pressed = new List<Keys>();
			this.m_state = Keyboard.GetState();
		}

		#endregion

		#region Properties

		/// <summary>
		/// Vrací stav klávesnice
		/// </summary>
		public KeyboardState State
		{
			get
			{
				return this.m_state;
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Metoda aktualizuje stav klávesnice
		/// </summary>
		public void Update()
		{
			this.m_state = Keyboard.GetState();
			Keys[] keys = this.m_state.GetPressedKeys();

			foreach (Keys key in keys)
			{
				if (this.m_pressed.Contains(key))
					continue;
				this.KeyPressed(this, new KeyEventArgs(key));
			}

			this.m_pressed.Clear();
			this.m_pressed.AddRange(keys);
		}

		#endregion

		#region Events

		/// <summary>
		/// Událost nastane po stisknutí kláves(y)
		/// </summary>
		public event EventHandler<KeyEventArgs> KeyPressed;

		#endregion


	}
}
